package com.fcat.base.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * window cmd命令行操作函数+后台日志打印
 */
public class CmdExecUtil {
    /**
     * 输出命令行执行的的后台日志
     * @param p
     */
    public static void execOutputLog(Process p){
        //取得命令结果的输出流
        InputStream fis=p.getInputStream();
        //用一个读输出流类去读
        InputStreamReader isr=new InputStreamReader(fis);
        //用缓冲器读行
        BufferedReader br=new BufferedReader(isr);
        String line=null;
        //直到读完为止
        try {
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 执行命令行
     * @param cmdLine
     * @return
     */
    public static String cmdExec(String cmdLine) throws IOException, InterruptedException {
//        String[] cmd = { "cmd", "/c", "mvn clean install -Dmaven.test.skip=true"};
        String[] cmd = { "cmd", "/c", cmdLine};
        Process p = Runtime.getRuntime().exec(cmd);//创建实例进程执行命令行代码
        execOutputLog(p);
        p.waitFor();
        p.destroy();
        return null;
    }
}
